<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/23
 * Time: 10:04
 * 老兵论坛
 */
namespace app\api\controller;
use think\Db;
class Veteran extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        header("Access-Control-Allow-Origin:*");
    
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 2111){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }
    }

    //列表
    public function index(){
        $page = input('param.page')?input('param.page'):1;
        $type = input('param.type')?input('param.type'):1;// 1政策解读  2答疑解惑
        $num = 10;
        $limit = $page-1;
        $where['is_publish'] = 1;
        if($type  == 1){
            $where['old_forum_type'] = 1;
        }elseif($type==2){
            $where['old_forum_type'] = 2;
        }
        $res = Db::table('up_old_forum')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.old_forum_user_id','left')
            ->field('a.*,uuu.user_username')
            ->where($where)
            ->limit($num*$limit,$num)
            ->order('a.old_forum_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            $info = Db::table('up_old_answer')->where('f_id',$vo['old_forum_id'])->order('old_answer_time desc')->find();
            $count = Db::table('up_old_answer')->where('f_id',$vo['old_forum_id'])->count();
            $vo['num'] = $count;
            $vo['info'] = $info;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //举报
    public function report(){
        extract(input());
        $r_type_id = input('param.id');
        $data['r_user_id'] = $user_id;
        $data['report_user_id'] = $report_user_id;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_type'] = 1;
        $data['r_type_id'] = $r_type_id;
        $data['report_intro'] = input('param.report_intro');
        $res = Db::table('up_report')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }

    //删除问题
    public function del(){
        extract(input());
        $id = input('param.id');
        $info = Db::table('up_old_forum')->where('old_forum_id',$id)->find();
        if($info['old_forum_user_id']!=$user_id){
            return  json(array('status'=>100,'msg'=>"不能删除不属于自己的悬赏问答"));
        }else{
            $info1 = Db::table('up_old_answer')->where('f_id',$id)->count();
            if($info1>=1){
                Db::table('up_old_answer')->where('f_id',$id)->delete();
            }
            $res = Db::table('up_old_forum')->where('old_forum_id',$id)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"删除成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"删除失败"));
            }
        }
    }

    //提问
    public function question(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名   2 匿名
        $is_publish = input('is_publish'); // 是否发布   1 发布   2 草稿箱
        $title = input('param.title');
        $answer_describe = input('param.describe');
        $title = $this->sensitive($title);
        $answer_describe = $this->sensitive($answer_describe);
        $img = input('param.img');
        $video = input('param.video');
        $type = input('param.type');
        $data['old_forum_title'] = $title;
        $data['old_forum_user_id'] = $user_id;
        $data['old_forum_time'] = date('Y-m-d H:i:s',time());
        $data['old_forum_content'] = $answer_describe;
        $data['old_forum_img'] = $img;
        $data['old_forum_video'] = $video;
        $data['old_forum_type'] = $type;
        $data['is_show_user'] = $is_show_user;
        $data['is_publish'] = $is_publish;
        $res = Db::table('up_old_forum')->insertGetId($data);
        return  json(array('status'=>200,'msg'=>$res));
    }

    //老兵论坛详情
    public function detail(){
        extract(input());
        $id = input('param.id');
        $res = Db::table('up_old_forum')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.old_forum_user_id','left')
            ->field('a.*,uuu.user_username')
            ->where('a.old_forum_id',$id)
            ->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        if($res){
            //这条最新的评论
            $info = Db::table('up_old_answer')->where('f_id',$res['old_forum_id'])->order('old_answer_time desc')->find();
            if($info){
                //评论用户信息
                $userInfo = Db::table('up_u_user')->where('user_id',$info['old_answer_user_id'])->field('user_username,user_image')->find();
                $info['user_username'] = $userInfo['user_username'];
                $info['user_image'] = $userInfo['user_image'];
                //多少评论
                $num = Db::table('up_old_answer')->where('f_id',$id)->count();
                $res['num'] = $num;
                //最新评论回复的踩多少人踩
                $where3['step_type'] = 2;
                $where3['f_id'] =$info['old_answer_id'];
                $num1 = Db::table('up_old_step')->where($where3)->count();
                $info['num1'] = $num1;
                //最新评论回复多少点赞
                $where4['give_type'] = 7;
                $where4['give_f_id'] = $info['old_answer_id'];
//        $where4['give_user_id'] = $user_id;
                $num2 = Db::table('up_comment_give')->where($where4)->count();
                $info['dz_num'] = $num2;
                //自己是否点赞
                $where1['give_type'] = 7;
                $where1['give_f_id'] = $info['old_answer_id'];
                $where1['give_user_id'] = $user_id;
                $info2 = Db::table('up_comment_give')->where($where1)->find();
                if($info2['give_id']!=''){//点赞
                    $info['is_dz'] = 1;
                }else{//没有点赞
                    $info['is_dz'] = 2;
                }
                //自己是否踩
                $where2['f_id'] = $info['old_answer_id'];
                $where2['step_user_id'] = $user_id;
                $where2['step_type'] = 2;
                $info1 = Db::table('up_old_step')->where($where2)->find();
    
                if($info1['step_id']!=''){//踩过
                    $info['is_step'] = 1;
                }else{//没有踩
                    $info['is_step'] = 2;
                }
                $res['info'] = $info;
                return json(['status' => 200, 'msg' => $res]);
            }else{
                $res['info'] = [];
                return json(['status' => 200, 'msg' => $res]);
            }
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

    //论坛回答发布
    public function answer(){
        extract(input());
        $video_id = input('param.id');
        $is_show_user = input('is_show_user'); // 是否匿名  1 不匿名  2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content);
        $img= input('param.img');
        $video = input('param.video');
        $data['f_id'] = $video_id;
        $data['old_answer_content'] = $content;
        $data['old_answer_video'] = $video;
        $data['old_answer_img'] = $img;
        $data['old_answer_time'] = date('Y-m-d H:i:s',time());
        $data['old_answer_user_id'] = $user_id;
        $data['is_show_user'] = $is_show_user;
        $res = Db::table('up_old_answer')->insertGetId($data);

        $data1['comment'] = $content;
        $data1['user_id'] = $user_id;
        $data1['time'] = date('Y-m-d H:i:s',time());
        $data1['type'] = 4;
        $data1['f_id'] = $video_id;//todo
        $data1['f_user_id'] = input('param.other_user');
        Db::table('up_comment')->insertGetId($data1);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //全部回答
    public function shareDetail(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_old_forum')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.old_forum_user_id','left')
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->where('a.old_forum_id',$id)
            ->find();
        //查询是否收藏这个资讯
        $whereMap4['collection_f_id'] = $id;
        $whereMap4['collection_type'] = 3;
        $whereMap4['collection_user_id'] = $user_id;
        $collection = Db::table('up_collection')->where($whereMap4)->find();
        if($collection['collection_id']!=''){
            $res['is_collection'] = 1;
        }else{
            $res['is_collection'] = 2;
        }
        //多少评论
        $num1 = Db::table('up_old_answer')->where('f_id',$id)->count();
        $res['num'] = $num1;
        //查询评论数据
        $info = Db::table('up_old_answer')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.old_answer_user_id','left')
            ->where('a.f_id',$id)
            ->limit($num*$limit,$num)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->select();
        foreach ($info as $key=>&$vo){
            //点赞数量
            $where1['give_type'] =7;
            $where1['give_f_id'] = $vo['old_answer_id'];
            $num3 = Db::table('up_comment_give')->where($where1)->count();
            //踩的数量
            $where2['step_type'] = 2;
            $where2['f_id'] = $vo['old_answer_id'];
            $num2 = Db::table('up_old_step')->where($where2)->count();
            $vo['dz'] = $num3;
            $vo['cai'] = $num2;

            //自己是否点赞
            $where3['give_type'] = 7;
            $where3['give_f_id'] = $vo['old_answer_id'];;
            $where3['give_user_id'] = $user_id;
            $info1 = Db::table('up_comment_give')->where($where3)->find();
            if($info1['give_id']!=''){//点赞
                $vo['is_dz'] = 1;
            }else{//没有点赞
                $vo['is_dz'] = 2;
            }
            //自己是否踩
            $where4['f_id'] = $vo['old_answer_id'];
            $where4['step_user_id'] = $user_id;
            $where4['step_type'] = 2;
            $info2 = Db::table('up_old_step')->where($where4)->find();
            if($info2['step_id']!=''){//踩过
                $vo['is_step'] = 1;
            }else{//没有踩
                $vo['is_step'] = 2;
            }
        }
        $res['data'] = $info;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //老兵论坛回复点赞
    public function oldDz(){
        // type  1评论回复点赞   2评论回复取消点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 7;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.user_id1');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =7;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }


    //踩，取消踩
    public function step2(){
        extract(input());
        $type  = input('param.type');
        if($type==1){

            $data['step_user_id'] = $user_id;
            $data['step_time'] = date('Y-m-d H:i:s', time());
            $data['f_id'] = input('param.id');
            $data['step_type'] = 2;
            $res = Db::table('up_old_step')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "踩成功"));
            } else {
                return json(array('status' => 200, 'msg' => "踩失败"));
            }
        }else{
            $data['step_user_id'] = $user_id;
            $data['f_id'] = input('param.id');
            $data['step_type'] = 2;
            $res = Db::table('up_old_step')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "踩成功"));
            } else {
                return json(array('status' => 200, 'msg' => "踩失败"));
            }

        }
    }

    //删除问题
    public function delAnswer(){
        extract(input());
        $id = input('param.id');
        $info = Db::table('up_old_answer')->where('old_answer_id',$id)->find();
        if($info['old_answer_user_id']!=$user_id){
            return  json(array('status'=>100,'msg'=>"不能删除不属于自己的悬赏问答"));
        }else{
            $res = Db::table('up_old_answer')->where('old_answer_id',$id)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"删除成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"删除失败"));
            }
         }
    }

    //举报
    public function reportAnswer(){
        extract(input());
        $r_type_id = input('param.id');
        $data['r_user_id'] = $user_id;
        $data['report_user_id'] = $report_user_id;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_type'] = 2;
        $data['r_type_id'] = $r_type_id;
        $data['report_intro'] = $report_intro;
        $res = Db::table('up_report')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }

    //收藏
    public function collection(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $id = input('param.id');
        $data['collection_f_id'] = $id;
        $data['collection_user_id'] = $user_id;
        $data['collection_type'] = 3;
        $res1 = Db::table('up_collection')->where($data)->find();
        if($res1['collection_id']!=''){
            $res= Db::table('up_collection')->where($data)->delete();
        }else{
            $data['collection_type']=3;
            $res= Db::table('up_collection')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "收藏成功"));
        } else {
            return json(array('status' => 200, 'msg' => "收藏失败"));
        }
    }
}
